<?php

namespace App\Http\Controllers\api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Models\Share;

class ShareController extends Controller
{

    /**
     * 广场红包列表
     * @return [type] [description]
     */
    public function listAll() {

        $shareList = Share::listAll();
        echo empty($shareList) ? json_encode(["code"=>201]) : json_encode(["code"=>200,"result"=>$shareList]);

    }

    public function detail(Request $request){
          $openid = $request->openid ;
              // $openid = 'okfAV0dqL0t4oeKw9zsB9_n4lgg8';
              $redfdata = DB::table('redf')->select('sign','created_at','price')->where([['openid','=',$openid],['status','=',1]])->get();
              $redsdata = DB::table('reds')->select('sign','price','created_at')->where([['openid','=',$openid],['sign','=',1]])->orWhere([['openid','=',$openid],['sign','=',2],['is_correct','=',1]])->get();
              $tixiandata = DB::table('tixian')->select('created_at','price')->where([['openid','=',$openid],['status','=',1]])->get();
              $shenhedata = DB::table('shenhe')->select('created_at','price')->where([['openid','=',$openid],['status','<>',1]])->get();

              foreach ($redfdata as $value) {
                      $value->type = 1 ;
                      $value->time = date('Y-m-d H:i:s',$value->created_at);
                      if ($value->sign == 1) {
                          $value->title = '发出语音口令';
                      }else {
                          $value->title = '发出猜对有钱';
                      }
              }
              foreach ($redsdata as $value) {
                      $value->time = date('Y-m-d H:i:s',$value->created_at);
                      $value->type = 0 ;
                       if ($value->sign == 1) {
                          $value->title = '语音口令';
                      }else {
                          $value->title = '猜对有钱';
                      }
              }
              foreach ($tixiandata as $value) {
                      $value->time = date('Y-m-d H:i:s',$value->created_at);
                      $value->title = '提现成功';
                      $value->type = 2 ;
                      $value->withdraw = 1;
              }
              foreach ($shenhedata as $value) {
                      $value->time = date('Y-m-d H:i:s',$value->created_at);
                      $value->type = 2;
                      if ($value->status == 0) {
                          $value->title = '提现中';
                         $value->withdraw = 0;
                      }elseif ($value->status == 2 || $value->status == 3) {
                          $value->title = '提现失败';
                         $value->withdraw = 2;
                         $value->failed = $value->content;
                      }
              }

              $redfdata = json_decode(json_encode($redfdata), true);
              $redsdata = json_decode(json_encode($redsdata), true);
              $tixiandata = json_decode(json_encode($tixiandata), true);
              $shenhedata = json_decode(json_encode($shenhedata), true);
              $data = array_merge($redfdata,$redsdata,$tixiandata,$shenhedata) ;
              $info = array_column($data, 'created_at');
              array_multisort($info,SORT_DESC,$data);
              echo json_encode($data);
    }
}
